package com.jhx.video.qualification

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.support.v4.view.PagerAdapter
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.jhx.common.activity.PicturePreviewActivity
import com.jhx.common.framework.QuickBaseFragment
import com.jhx.common.utils.DisplayUtil
import com.jhx.common.utils.ImageLoader
import com.jhx.common.utils.UserManager
import com.jhx.video.R
import kotlinx.android.synthetic.main.fragment_photo.*

/**
 *
 *
 * @author Jack 2018-03-29 15:16
 */
class QualificationFragment : QuickBaseFragment() {
    private var mPhotoList: ArrayList<String> = ArrayList()
    private var mAdapter: Adapter? = null
    private var mEmptyView: View? = null

    override fun bindView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.fragment_photo, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mEmptyView = view.findViewById(R.id.empty_layout)
    }

    @SuppressLint("MissingSuperCall")
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        txt_tip.visibility = View.VISIBLE
        val user = UserManager.get().user
        if(!TextUtils.isEmpty(user?.license)) {
            mPhotoList.add(user.license)
            mAdapter = Adapter()
            viewpager.adapter = mAdapter
            txt_number.text = "1/1"
        } else {
            txt_number.visibility = View.GONE
            viewpager.visibility = View.GONE
            mEmptyView?.visibility = View.VISIBLE
        }
    }

    inner class Adapter : PagerAdapter() {
        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view == `object`
        }

        override fun getCount(): Int {
            return mPhotoList.size
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val view = ImageView(activity)
            view.setOnClickListener {
                val intent = Intent(activity, PicturePreviewActivity::class.java)
                val urlList = ArrayList<String>()
                mPhotoList.forEach {
                    urlList.add(it)
                }
                intent.putExtra(PicturePreviewActivity.KEY_DATA, urlList)
                intent.putExtra(PicturePreviewActivity.KEY_INDEX, position)
                startActivity(intent)
            }
            val params = ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.MATCH_PARENT, DisplayUtil.dip2px(activity, 200f))
            view.layoutParams = params
            ImageLoader.get().loadImageByUrl(activity, mPhotoList[position], view)
            container.addView(view, position)
            return view
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeViewAt(position)
        }

        override fun getItemPosition(`object`: Any): Int {
            return POSITION_NONE
        }
    }

}